import scrapy


class GzSpider(scrapy.Spider):
    name = 'gz'
    allowed_domains = ['guazi.com']
    start_urls = ['https://www.guazi.com/yueyang/buy/o1/#bread']
    page_num = 1

    def parse(self, response):
        print('====================>', response)
        li_list = response.xpath('/html/body/div[6]/ul/li')
        for li_temp in li_list:
            car_title = li_temp.xpath('./a/h2/text()').extract_first()
            car_year = li_temp.xpath('./a/div[1]/text()[1]').extract_first()
            car_mileage = li_temp.xpath('./a/div[1]/text()[2]').extract_first()
            car_price = li_temp.xpath('./a/div[2]/p/text()').extract_first()
            car_price_old = li_temp.xpath('./a/div[2]/em/text()').extract_first()
            # print(car_title, car_year, car_mileage, car_price, car_price_old)
            yield {
                'car_title': car_title,
                'car_year': car_year,
                'car_mileage': car_mileage,
                'car_price': car_price + '万',
                'car_price_old': car_price_old
            }
        self.page_num += 1
        if self.page_num <= 10:
            print(self.page_num)
            next_page_url = 'https://www.guazi.com/yueyang/buy/o%s/#bread' % self.page_num
            yield scrapy.Request(url=next_page_url, callback=self.parse)
